from .base import IDEPlugin, PagePlugin
from .search_widget import SearchPanel
import wx

class SelectAll(PagePlugin):
    name = 'Select All'

    def run(self, app, page, para=None):
        page.get_page().SelectAll()
        
class Copy(PagePlugin):
    name = 'Copy'

    def run(self, app, page, para=None):
        page.get_page().Copy()

class Cut(PagePlugin):
    name = 'Cut'

    def run(self, app, page, para=None):
        page.get_page().Cut()
        
class Paste(PagePlugin):
    name = 'Paste'

    def run(self, app, page, para=None):
        page.get_page().Paste()
        
class Comment(PagePlugin):
    name = 'Comment'

    def run(self, app, page, para=None):
        page.get_page().comment(True)

class UnComment(PagePlugin):
    name = 'Comment'

    def run(self, app, page, para=None):
        page.get_page().comment(False)
        
class Indent(PagePlugin):
    name = 'Indent'

    def run(self, app, page, para=None):
        page.get_page().Tab()

class Dedent(PagePlugin):
    name = 'Dedent '

    def run(self, app, page, para=None):
        page.get_page().BackTab()

class Search(PagePlugin):
    name = 'Search'

    def run(self, app, page, para=None):
        cont = page.get_page().GetSelectedText()
        SearchPanel.show(app, cont if len(cont)>0 else None, True)

class NewFile(PagePlugin):
    name = 'New'

    def run(self, app, page, para=None):
        app.show_code('# Hello world!')

class OpenFile(PagePlugin):
    name = 'New'

    def run(self, app, page, para=None):
        path = app.get_path('打开文件', ['py'], 'open')
        if not path is None: app.on_open(path, True)

class OpenDir(IDEPlugin):
    name = 'Open Dir'

    def run(self, app, page, console, workspace, catlog, para):
        path = app.get_path('打开目录', ['py'], 'dir')
        if not path is None: catlog.AppendRoot(path)

class SaveFile(PagePlugin):
    name = 'Save'

    def run(self, app, page, para=None):
        page.get_page().Save()

class ExecFile(IDEPlugin):
    name = 'Run File'

    def run(self, app, page, console, workspace, catlog, para):
        app.run_button_enable([0,1,0,0,0,0,0,0])
        
        if 'page.code.path' not in dir():
            page.get_page().Save()
            return 0

        console, page = console.get_console(), page.get_page()
        console.write("execfile('%s')\n"%page.code.path.replace('\\','/'))
        console.execfile(page.code.path, lambda x : 
            app.run_button_enable([1,1,1,0,0,0,0,0]))

class Terminate(IDEPlugin):
    name = 'Run File'

    def run(self, app, page, console, workspace, catlog, para):
        console.get_console().restart()
        app.run_button_enable([1,1,1,0,0,0,0,0])

class BreakPoint(IDEPlugin):
    name = 'Break Point'
    def run(self, app, page, console, workspace, catlog, para):
        console = console.get_console()
        breakpoints = []
        for i in range(page.GetPageCount()):
            p = page.GetPage(i)
            if hasattr(p, 'breakpoints'):
                breakpoints.extend([(p.code.path, ln) for ln in p.breakpoints])
        console.debug('breakpoint', breakpoints)

class Debug(IDEPlugin):
    name = 'Debug'

    def run(self, app, page, console, workspace, catlog, para):
        app.run_button_enable([0,1,0,1,1,1,1,1])
        console, page = console.get_console(), page.get_page()
        console.write("debug('%s')\n"%page.code.path.replace('\\','/'))
        console.pc.enddebug = lambda x: wx.CallAfter(app.run_button_enable, [1,1,1,0,0,0,0,0])
        def callback():
            BreakPoint().start(app)
            page.highlight_line(1)
            import time; time.sleep(0.1)
            workspace.on_fresh(None)
        console.debug('exec', page.code.path, lambda x: wx.CallAfter(callback))
        
def debug_step(app, page, work, info):
    app.on_open(info['path'], False)
    page.get_page().highlight_line(info['no'])
    app.run_button_enable([0,1,0,1,1,1,1,1])
    work.on_fresh(None)

class DebugContinue(IDEPlugin):
    name = 'Continue'

    def run(self, app, page, console, workspace, catlog, para):
        app.run_button_enable([0,1,0,0,0,0,0,1])
        console = console.get_console()
        console.debug('action', ':c',  lambda x: 
            wx.CallAfter(debug_step, app, page, workspace, x[0]))

class DebugNext(IDEPlugin):
    name = 'Next'

    def run(self, app, page, console, workspace, catlog, para):
        app.run_button_enable([0,1,0,0,0,0,0,1])
        console = console.get_console()
        console.debug('action', ':n',  lambda x: 
            wx.CallAfter(debug_step, app, page, workspace, x[0]))

class DebugInto(IDEPlugin):
    name = 'Step Into'

    def run(self, app, page, console, workspace, catlog, para):
        app.run_button_enable([0,1,0,0,0,0,0,1])
        console = console.get_console()
        console.debug('action', ':s',  lambda x: 
            wx.CallAfter(debug_step, app, page, workspace, x[0]))

class DebugOut(IDEPlugin):
    name = 'Step Out'

    def run(self, app, page, console, workspace, catlog, para):
        app.run_button_enable([0,1,0,0,0,0,0,1])
        console = console.get_console()
        console.debug('action', ':r',  lambda x: 
            wx.CallAfter(debug_step, app, page, workspace, x[0]))

class DebugStop(IDEPlugin):
    name = 'Debug Stop'

    def run(self, app, page, console, workspace, catlog, para):
        app.run_button_enable([0,1,0,0,0,0,0,1])
        console = console.get_console()
        console.debug('action', ':q',  lambda x: 
            wx.CallAfter(debug_step, app, page, workspace, x[0]))